package cn.rongdao.jrkabao.ui.information.uicomponent;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.information.QuotaBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.ui.information.listener.OnItemActionListener;


/**
 * Created by Wang on 17/10/25.
 * 我的额度界面的完善资料列表的item的所有布局和逻辑.
 */

public class MyQuotaItemComponent extends RelativeLayout {

    private LinearLayout mNomatch;

    public MyQuotaItemComponent(Context context) {
        super(context);
    }

    public MyQuotaItemComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyQuotaItemComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int[] normal = {
            R.drawable.icon_quota_realname,
            R.drawable.icon_quota_creditcard_,
            R.drawable.icon_quota_providentfund,
            R.drawable.icon_quota_baseinformation,
            R.drawable.icon_quota_carriers,
            R.drawable.icon_quota_taobao,
            R.drawable.icon_quota_zhimafen,
            R.drawable.icon_quota_identificationphoto
    };
    int[] complete = {
            R.drawable.icon_quota_realname_complete,
            R.drawable.icon_quota_creditcard_complete,
            R.drawable.icon_quota_providentfund_complete,
            R.drawable.icon_quota_baseinformation_complete,
            R.drawable.icon_quota_carriers_complete,
            R.drawable.icon_quota_taobao_complete,
            R.drawable.icon_quota_zhimafen_complete,
            R.drawable.icon_quota_identificationphoto_complete
    };

    private RelativeLayout item;
    private ImageView icon;
    private TextView name;
    private TextView state;
    private LinearLayout valitation;
    private TextView mTageHeightQuate; //最高可贷额度
    private OnItemActionListener meOnClickListener;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        item = (RelativeLayout) findViewById(R.id.item_myquota_item);
        icon = (ImageView) findViewById(R.id.item_myquota_icon_img);
        name = (TextView) findViewById(R.id.item_myquota_name_tv);
        state = (TextView) findViewById(R.id.item_myquota_state_tv);
        mTageHeightQuate = (TextView) findViewById(R.id.item_myquota_text_heightquate);//最高可贷额度
        valitation = (LinearLayout) findViewById(R.id.item_myquota_state_valitation);
        mNomatch = (LinearLayout) findViewById(R.id.item_myquota_state_nomatch);
    }

    /*设置Item的高度*/
    public void setItemHeight(float ScreenHeoght) {
        if (ScreenHeoght > 1) {
            LayoutParams lps = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) ScreenHeoght - 3);
            item.setLayoutParams(lps);
        }
    }

    /*设置shuju */
    public void setQuotaBeanValidationBean(final QuotaBean.ValidationBean validationBean, int position) {
        String material_code = validationBean.getMaterial_code();
        name.setText(validationBean.getCn());
        String value = validationBean.getValue();
        setTageHeightQuate(value);
        int verify_status = validationBean.getVerify_status();
        //1 通过 -1 正在验证 2 实名不匹配  0  没用填过资料1:
        setIconAndText(icon, name, verify_status, material_code, position);

        switch (verify_status) {
            case 0:
                //实名不匹配 显示不匹配标签
                state.setBackgroundResource(getDrawable(0));
                state.setTextColor(getTextColor(2));
                state.setText("去验证");
                state.setVisibility(View.VISIBLE);
                valitation.setVisibility(View.INVISIBLE);
                mNomatch.setVisibility(View.INVISIBLE);
                break;
            case 1:
                //实名通过显示已验证标签
                mTageHeightQuate.setVisibility(INVISIBLE);
                state.setBackgroundResource(getDrawable(1));
                state.setTextColor(getTextColor(3));
                state.setText("已验证");
                state.setVisibility(View.VISIBLE);
                valitation.setVisibility(View.INVISIBLE);
                mNomatch.setVisibility(View.INVISIBLE);
                break;
            case 2:
                //实名不匹配 显示不匹配标签
                if (mNomatch.getVisibility() == VISIBLE)
                    break;
                mNomatch.setVisibility(View.VISIBLE);
                name.setTextColor(getTextColor(0));
                state.setVisibility(View.INVISIBLE);
                valitation.setVisibility(View.INVISIBLE);
                break;
            case -1:
                //正在验证
                if (valitation.getVisibility() == VISIBLE)
                    break;
                valitation.setVisibility(View.VISIBLE);
                name.setTextColor(getTextColor(0));
                state.setVisibility(View.INVISIBLE);
                mNomatch.setVisibility(View.INVISIBLE);
                break;
        }
        item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (meOnClickListener != null) {
                    initListener(validationBean);
                }
            }
        });
    }

    private void initListener(QuotaBean.ValidationBean validationBean) {
        initNativeClickListener(validationBean);
    }

    /*设置最高可贷多少*/
    private void setTageHeightQuate(String text) {
        mTageHeightQuate.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            mTageHeightQuate.setText(text);
        } else {
            mTageHeightQuate.setText("");
        }
    }

    /*点击事件*/
    private void initNativeClickListener(QuotaBean.ValidationBean validationBean) {
        switch (validationBean.getMaterial_code()) {
            case InfoConstant.IDENTITY_INFO://身份信息
                meOnClickListener.onIdentityInfoClick(validationBean.getVerify_status());
                break;
            case InfoConstant.BASIC_INFO://基本资料
                meOnClickListener.onBasicInfoClick(validationBean.getVerify_status());
                break;
            case InfoConstant.CREDIT_VERIFY://信用卡
                meOnClickListener.onCreditVerifyClick(validationBean.getVerify_status());
                break;
            case InfoConstant.DEPOSIT_CARD://储蓄卡
                meOnClickListener.onDepositCardClick(validationBean.getVerify_status());
                break;
            case InfoConstant.EC_VERIFY://电商
                meOnClickListener.onECVerifyClick(validationBean.getVerify_status());
                break;
            case InfoConstant.IDENTITY_PIC://身份证照骗
                meOnClickListener.onIdentityPicClick(validationBean.getVerify_status());
                break;
            case InfoConstant.OPERATOR_VERIFY://手机号验证
                meOnClickListener.onOperatorVerifyClick(validationBean.getVerify_status());
                break;
            case InfoConstant.PERSONAL_CREDIT://个人征信
                meOnClickListener.onPersonalCreditClick(validationBean.getVerify_status());
                break;
            case InfoConstant.PUBLIC_ACCUMULATION_FUND://公积金
                meOnClickListener.onPublicAccumulationFundClick(validationBean.getVerify_status());
                break;
            case InfoConstant.ZHIMA://芝麻信用
                meOnClickListener.onZhimaClick(validationBean.getVerify_status());
                break;
            case InfoConstant.JD_VERIFY://京东
                meOnClickListener.onJingdongClick(validationBean.getVerify_status());
                break;
            case InfoConstant.OTHER_FILM_PROFILE://资产证明
                meOnClickListener.onOtherFilmprofileClick(validationBean.getVerify_status());
                break;
            case InfoConstant.ALIPAY://资产证明
                meOnClickListener.onAlipayClick(validationBean.getVerify_status());
                break;
            case InfoConstant.SECURITY_VERIFY://社保
                meOnClickListener.onSecurityVerifyClick(validationBean.getVerify_status());
                break;


        }
    }

    /*设置图标和那么*/
    private void setIconAndText(ImageView icon, TextView name, int status, String tage, int position) {
        if (1 == status) {
            //验证已通过
            name.setTextColor(getTextColor(1));
        } else {
            name.setTextColor(getTextColor(0));
        }
        icon.setImageResource(getIconFromTag(tage, status, position));
    }

    /*获取图标*/
    private int getIconFromTag(String tage, int status, int position) {
        switch (tage) {
            //实名认证
            case InfoConstant.IDENTITY_INFO:
                if (status == 1) {
                    return complete[0];
                } else {
                    return normal[0];
                }
                //信用卡
            case InfoConstant.CREDIT_VERIFY:
                if (status == 1) {
                    return complete[1];
                } else {
                    return normal[1];
                }
                //公积金
            case InfoConstant.PUBLIC_ACCUMULATION_FUND:
                if (status == 1) {
                    return complete[2];
                } else {
                    return normal[2];
                }
            case "public_fund":
                if (status == 1) {
                    return complete[2];
                } else {
                    return normal[2];
                }
                //基本资料
            case "jiabenziliao":
                if (status == 1) {
                    return complete[3];
                } else {
                    return normal[3];
                }
            case InfoConstant.BASIC_INFO:
                if (status == 1) {
                    return complete[3];
                } else {
                    return normal[3];
                }
                //运营商
            case InfoConstant.OPERATOR_VERIFY:
                if (status == 1) {
                    return complete[4];
                } else {
                    return normal[4];
                }
                //淘宝
            case InfoConstant.EC_VERIFY:
                if (status == 1) {
                    return complete[5];
                } else {
                    return normal[5];
                }
                //芝麻分
            case InfoConstant.ZHIMA:
                if (status == 1) {
                    return complete[6];
                } else {
                    return normal[6];
                }
                //身份证照片
            case InfoConstant.IDENTITY_PIC:
                if (status == 1) {
                    return complete[7];
                } else {
                    return normal[7];
                }
            case "shenfenzh":
                if (status == 1) {
                    return complete[7];
                } else {
                    return normal[7];
                }
            default:
                return normal[position];
        }
    }

    /*选择颜色*/
    private int getTextColor(int status) {
        switch (status) {
            case 0:
                return Color.parseColor("#b0b0b0");
            case 1:
                return Color.parseColor("#666666");

            case 2:
                return Color.parseColor("#ff5a6f");
            case 3:
                return Color.parseColor("#23bd9f");
            default:
                return 0;

        }
    }

    /*选择背景样式*/
    private int getDrawable(int tage) {
        if (tage == 1) {
            return R.drawable.bg_100_23bd9f;
        } else {
            return R.drawable.bg_100_ff5a6f;
        }
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.meOnClickListener = onItemActionListener;
    }
}
